/*
 * Copyright 2009-2010 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.web.opensearch.objectsource;

import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.Icon;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * @author Peter De Bruycker
 */
public class OpenSearch {
    private String id;
    private String name;
    private Icon icon;
    private String description;
    private String urlTemplate;

    public OpenSearch(String id, Document document) {
        this.id = id;
        Element root = document.getDocumentElement();
        Element shortNameElement = (Element) root.getElementsByTagName("ShortName").item(0);
        name = shortNameElement.getTextContent();

        Element descriptionElement = (Element) root.getElementsByTagName("Description").item(0);
        description = descriptionElement.getTextContent();

        Element urlElement = (Element) root.getElementsByTagName("Url").item(0);
        urlTemplate = urlElement.getAttribute("template");

        // TODO handle method post?
        NodeList paramNodeList = urlElement.getElementsByTagName("Param");
        for (int i = 0; i < paramNodeList.getLength(); i++) {
            Element paramElement = (Element) paramNodeList.item(i);

            if (i == 0) {
                urlTemplate = urlTemplate + "?";
            } else {
                urlTemplate = urlTemplate + "&";
            }
            urlTemplate = urlTemplate + paramElement.getAttribute("name") + "=" + paramElement.getAttribute("value");
        }

        // favicon
        NodeList imageElements = root.getElementsByTagName("Image");
        if (imageElements.getLength() > 0) {
            Element imageElement = (Element) imageElements.item(0);
            int width = Integer.parseInt(imageElement.getAttribute("width"));
            for (int i = 1; i < imageElements.getLength(); i++) {
                Element next = (Element) imageElements.item(i);
                if (Integer.parseInt(next.getAttribute("width")) > width) {
                    imageElement = next;
                }
            }

            // try {
            // System.out.println(imageElement.getTextContent());
            // BufferedImage favIconImage = ImageIO.read(new URL(imageElement.getTextContent()));
            // icon = new ImageIcon(favIconImage);
            // } catch (MalformedURLException e) {
            // // TODO Auto-generated catch block
            // e.printStackTrace();
            // } catch (DOMException e) {
            // // TODO Auto-generated catch block
            // e.printStackTrace();
            // } catch (IOException e) {
            // // TODO Auto-generated catch block
            // e.printStackTrace();
            // }
        }
    }

    public String getId() {
        return id;
    }

    public String getDescription() {
        return description;
    }

    public String getName() {
        return name;
    }

    public Icon getIcon() {
        return icon;
    }

    public String createUrl(String searchTerms) {
        String url = urlTemplate.replaceAll("\\{searchTerms\\}", searchTerms);

        try {
            return new URI(null, url, null).toASCIIString();
        } catch (URISyntaxException e) {
            throw new RuntimeException("this should not happen", e);
        }
    }

}
